/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.wechatmsg.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoUserService;
import com.lczyfz.campusinfo.modules.user.utils.ServerResponse;
import com.lczyfz.campusinfo.modules.wechatmsg.service.WeixinGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.wechatmsg.entity.WeixinMsg;
import com.lczyfz.campusinfo.modules.wechatmsg.service.WeixinMsgService;

import java.util.HashMap;
import java.util.List;

/**
 * 微信消息Controller
 * @author 陈维乾
 * @version 2018-05-07
 */
@Controller
@RequestMapping(value = "${adminPath}/wechatmsg/weixinMsg")
public class WeixinMsgController extends BaseController {

	@Autowired
	private WeixinMsgService weixinMsgService;

	@Autowired
	private CampusinfoUserService campusinfoUserService;

	@Autowired
	private WeixinGroupService weixinGroupService;

	@ModelAttribute
	public WeixinMsg get(@RequestParam(required=false) String id) {
		WeixinMsg entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = weixinMsgService.get(id);
		}
		if (entity == null){
			entity = new WeixinMsg();
		}
		return entity;
	}
	
	@RequiresPermissions("wechatmsg:weixinMsg:view")
	@RequestMapping(value = {"list", ""})
	public String list(WeixinMsg weixinMsg, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WeixinMsg> page = weixinMsgService.findPage(new Page<WeixinMsg>(request, response), weixinMsg); 
		model.addAttribute("page", page);
		return "modules/wechatmsg/weixinMsgList";
	}

	@RequiresPermissions("wechatmsg:weixinMsg:view")
	@RequestMapping(value = "form")
	public String form(WeixinMsg weixinMsg, Model model) {
		List<User> userList = campusinfoUserService.getWeiXinUser();
		model.addAttribute("userList", userList);
		HashMap<String,List<User>> stringListHashMap = weixinGroupService.getGroupUserMaps();

		model.addAttribute("stringListHashMap", stringListHashMap);

		model.addAttribute("weixinMsg", weixinMsg);
		return "modules/wechatmsg/weixinMsgForm";
	}

	@RequiresPermissions("wechatmsg:weixinMsg:edit")
	@RequestMapping(value = "save")
	public String save(WeixinMsg weixinMsg, Model model, RedirectAttributes redirectAttributes) {

//		System.err.println(weixinMsg);
//		String str[] = weixinMsg.getRecevier();
//		int i= 0;
//		for(String s:str){
//			System.err.println(s);
//			i++;
//		}
//		System.err.println(i);
		if (!beanValidator(model, weixinMsg)){
			return form(weixinMsg, model);
		}
		ServerResponse serverResponse = weixinMsgService.sendMsgs(weixinMsg);
		if(serverResponse.isSuccess()){
			addMessage(redirectAttributes, "发送成功,"+serverResponse.getMsg());
			weixinMsgService.save(weixinMsg);
		}else{
			addMessage(redirectAttributes, "发送失败,"+serverResponse.getMsg());
		}
		return "redirect:"+Global.getAdminPath()+"/wechatmsg/weixinMsg/?repage";
	}
	
	@RequiresPermissions("wechatmsg:weixinMsg:edit")
	@RequestMapping(value = "delete")
	public String delete(WeixinMsg weixinMsg, RedirectAttributes redirectAttributes) {
		weixinMsgService.delete(weixinMsg);
		addMessage(redirectAttributes, "删除微信消息成功");
		return "redirect:"+Global.getAdminPath()+"/wechatmsg/weixinMsg/?repage";
	}

}